using System;
using System.Collections;
using System.Web;

namespace app.scrum
{
    public partial class Product : ScrumPage
    {
        public Product()
        {
            this.sObject.tableName = ScrumObject.objectName_Product;
        }

        public override void read(HttpContext context)
        {
            try
            {
                base.read(context);
            }
            catch (Exception ex)
            {
                this.handleException(context.Response, ex);
            }
        }

        public override void create(HttpContext context, ArrayList sObjList)
        {
            try
            {
                int num = 0;
                long maxId = 0;
                foreach (ScrumObject sObj in sObjList)
                {
                    if (maxId == 0)
                    {
                        maxId = this.sObject.getMaxId();
                    }
                    maxId++;
                    ScrumField id = new ScrumField();
                    id.name = ScrumField.fieldName_Id;
                    id.value = maxId;
                    sObj.addField(sObj, id);
                    this.sObject.genCreateCmdText(sObj);
                    ScrumField startDate = (ScrumField)sObj.fieldMap[ScrumField.fieldName_StartDate];
                    num = this.sObject.create(sObj);

                }
                context.Response.Write(num);
            }
            catch (Exception ex)
            {
                this.handleException(context.Response, ex);
            }
        }

        public override void update(HttpContext context, ArrayList sObjList)
        {
            try
            {
                int num = 0;
                foreach (ScrumObject sObj in sObjList)
                {
                    //this.sObject.genUpdateCmdText(sObj);
                    this.sObject.updateCmdText = "update product__c set desc=";
                    foreach (ScrumField sField in sObj.fieldList)
                    {
                        if (Util.isEmpty(sField.typeName))
                        {
                            if (String.Equals(sField.name, "startdate") || String.Equals(sField.name, "enddate"))
                            {
                                sField.typeName = "DateTime";
                            }
                            else
                            {
                                sField.typeName = "String";
                            }
                        }
                    }
                    this.sObject.updateCmdText += "'"+((ScrumField)sObj.fieldMap["desc"]).value +"'";
                    ArrayList fieldList = new ArrayList(sObj.fieldList);
                    foreach (ScrumField sField in fieldList)
                    {
                        //if (!String.Equals(sField.name, "desc"))
                        //{
                            sObj.fieldList.Remove(sField);
                        //}
                    }
                    num = this.sObject.update(sObj);
                }
                context.Response.Write(num);
            }
            catch (Exception ex)
            {
                this.handleException(context.Response, ex);
            }
        }
    }
}